﻿package frEngine.loaders.resource.info
{
    import __AS3__.vec.*;
    import com.gengine.debug.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.loaders.away3dMd5.*;

    public class SkeletonInfo extends Info3dBase
    {
        public var mainParser:MD5AnimByteArrayParser;
        public var subSkeletonMap:Dictionary;
        private var _subParserList:Vector.<MD5SubAnimParser>;
        public var userList:Dictionary;

        public function SkeletonInfo(param1:Object = null)
        {
            this.subSkeletonMap = new Dictionary(false);
            this._subParserList = new Vector.<MD5SubAnimParser>;
            this.userList = new Dictionary(false);
            super(param1);
            this.mainParser = new MD5AnimByteArrayParser(this.name);
            return;
        }// end function

        override public function dispose() : void
        {
            var _loc_1:Object = null;
            var _loc_2:DataInfo = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:MD5SubAnimParser = null;
            for each (_loc_1 in this.userList)
            {
                
                return;
            }
            this.mainParser.clear();
            proceedParsed = false;
            for each (_loc_2 in this.subSkeletonMap)
            {
                
                LoaderManager.instance.removeResourceEvent(_loc_2.name, this.subSkeletonLoaded);
                _loc_2.clearCacheBytes();
                _loc_2.dispose();
            }
            this.subSkeletonMap = new Dictionary(false);
            _loc_3 = this._subParserList.length;
            _loc_4 = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = this._subParserList[_loc_4];
                _loc_5.clear();
                _loc_4++;
            }
            this._subParserList = new Vector.<MD5SubAnimParser>;
            super.dispose();
            return;
        }// end function

        public function loadSubSkeleton(param1:String) : void
        {
            var _loc_3:String = null;
            if (this.mainParser.headAndJoins == null)
            {
                return;
            }
            var _loc_2:* = this.subSkeletonMap[param1];
            if (_loc_2 == null)
            {
                _loc_3 = this.name.replace(/\.skeleton""\.skeleton/i, "");
                _loc_3 = _loc_3 + "___" + param1 + ".data";
                _loc_2 = ResourceManager.getInfoByName(_loc_3);
                if (!_loc_2)
                {
                    _loc_2 = new DataInfo(null);
                    _loc_2.name = _loc_3;
                    _loc_2.type = ".data";
                    var _loc_4:* = this.soPath.replace(this.name, "") + _loc_3;
                    _loc_2.loaclPath = this.soPath.replace(this.name, "") + _loc_3;
                    _loc_2.path = _loc_4;
                    ResourceManager.addResource(_loc_2);
                }
                this.subSkeletonMap[param1] = _loc_2;
                LoaderManager.instance.load(_loc_3, this.subSkeletonLoaded, 0);
            }
            return;
        }// end function

        private function subSkeletonLoaded(param1:DataInfo) : void
        {
            if (this.mainParser.headAndJoins == null)
            {
                return;
            }
            var _loc_2:* = param1.name;
            var _loc_3:* = _loc_2.split("___");
            var _loc_4:* = _loc_3[(_loc_3.length - 1)];
            _loc_4 = _loc_4.replace(".data", "");
            proceedParsed = false;
            var _loc_5:* = this.mainParser.animationSet.getSubClipNode(_loc_4);
            this._subParserList.push(new MD5SubAnimParser(this.name, this.mainParser, param1.byteArray, _loc_5));
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            proceedParsed = false;
            this.mainParser.name = this.name;
            var _loc_2:* = this.mainParser.proceedParsingHead(_byteArray);
            if (!_loc_2)
            {
                _byteArray = null;
                Log.error("骨骼解析出错:" + this.name);
            }
            return;
        }// end function

        override public function proceedParsing() : void
        {
            var _loc_3:MD5SubAnimParser = null;
            var _loc_4:Boolean = false;
            if (proceedParsed || !_byteArray || _byteArray.length == 0)
            {
                return;
            }
            if (!this.mainParser.paserCompleted)
            {
                proceedParsed = this.mainParser.proceedParsing();
                if (proceedParsed)
                {
                    FrEventDispatcher.instance.proxyDispatchEvent(this, Engine3dEventName.PARSE_RESOURCE_FINISH);
                }
            }
            else
            {
                proceedParsed = true;
            }
            var _loc_1:* = this._subParserList.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._subParserList[_loc_2];
                _loc_4 = _loc_3.proceedParsing();
                if (_loc_4)
                {
                    this._subParserList.splice(_loc_2, 1);
                    _loc_2 = _loc_2 - 1;
                    _loc_1 = _loc_1 - 1;
                }
                proceedParsed = proceedParsed && _loc_4;
                _loc_2++;
            }
            return;
        }// end function

    }
}
